package com.naiterui.ehp.bs.patient.controller.inner;

import com.alibaba.fastjson.JSONObject;
import com.naiterui.ehp.bp.bo.patient.MsgMaPublishBatchParamBO;
import com.naiterui.ehp.bp.bo.patient.MsgMaPublishParamBO;
import com.naiterui.ehp.bp.bo.patient.MsgPublishBO;
import com.naiterui.ehp.bp.bo.patient.MsgPublishBatchParamBO;
import com.naiterui.ehp.bp.bo.patient.MsgPublishParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.message.IMessageSendService;
import com.naiterui.ehp.bs.patient.vo.message.MessageVo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Date:2018/11/1
 * @Time:10:02
 * @Version:V1.0
 */
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.MESSAGE_SEND})
@RestController
@RequestMapping("ap/inner")
public class ApInnerMessageSendController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApInnerMessageSendController.class);

    private final IMessageSendService messageSendService;

    public ApInnerMessageSendController(IMessageSendService messageSendService) {
        this.messageSendService = messageSendService;
    }

    /**
     * 推送模板消息
     *
     * @param messageVo
     *
     * @return
     */
    @RequestMapping("/push")
    public ResponseVO<Boolean> sendMessage(MessageVo messageVo) {

        int code = ExceptionCodes.SUCCESS;
        String msg = null;
        LOGGER.info("推送模板接口参数为：[{}]", JSONObject.toJSONString(messageVo));
        if (null != messageVo) {
            if (null != messageVo.getTemplateId() && null != messageVo.getPlatformId() && null != messageVo.getParamData()) {
                LOGGER.info("推送模板接口参数为：[{}]", messageVo.toString());
                code = messageSendService.sendMessage(messageVo);
            } else if (null == messageVo.getTemplateId()) {
                code = ExceptionCodes.DATA_ERR;
                LOGGER.info("推送模板接口失败,templateId 不能为空");
                msg = "templateId 不能为空";
            } else if (null == messageVo.getPlatformId()) {
                code = ExceptionCodes.DATA_ERR;
                LOGGER.info("推送模板接口失败,platformId 不能为空");
                msg = "platformId 不能为空";
            } else if (null == messageVo.getParamData()) {
                code = ExceptionCodes.DATA_ERR;
                LOGGER.info("推送模板接口失败,paramData 不能为空");
                msg = "paramData 不能为空";
            }
        } else {
            code = ExceptionCodes.DATA_ERR;
            LOGGER.info("推送模板接口失败,参数 不能为空");
            msg = "参数不能为空";
        }
        return PublicService.returnResponseVO(code, msg, true);
    }

    @RequestMapping("/message/publish")
    public ResponseVO<MsgPublishBO> publishMessage(@RequestBody MsgPublishParamBO publishParamBO) {
        MsgPublishBO msgPublishBO = messageSendService.publishMessage(publishParamBO);
        return PublicService.returnResponseVO(msgPublishBO);
    }

    @RequestMapping("/message/publish/batch")
    public ResponseVO<Void> publishBatchMessage(@RequestBody MsgPublishBatchParamBO publishBatchParamBO) {
        messageSendService.publishBatchMessage(publishBatchParamBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    /**
     * 发布小程序消息
     * @param publishParamBO
     * @return
     */
    @PostMapping("/message/ma/publish")
    public ResponseVO<MsgPublishBO> publishMessage(@RequestBody MsgMaPublishParamBO publishParamBO) {
        MsgPublishBO msgPublishBO = messageSendService.publishMaMessage(publishParamBO);
        return PublicService.returnResponseVO(msgPublishBO);
    }

    /**
     * 批量发布小程序消息
     * @param publishBatchParamBO
     * @return
     */
    @PostMapping("/message/ma/publish/batch")
    public ResponseVO<Void> publishBatchMaMessage(@RequestBody MsgMaPublishBatchParamBO publishBatchParamBO) {
        messageSendService.publishBatchMaMessage(publishBatchParamBO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
